import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/common/dialogs/message_dialog.dart';
import 'package:xinxuan/common/dialogs/union_pay_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/vo/blindbox/blindbox_products_detail_vo.dart';
import 'package:xinxuan/vo/blindbox/blindbox_submit_order_vo.dart';

class BlindBoxDetailPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();

  var data = BlindboxProductsDetailVo(
    data: BlindboxProductsData(
      id: 0, 
      images: [], 
      title: '', 
      price: '', 
      sales: 0, 
      status: 0, 
      remainingTime: 0, 
      rewardRules: '', 
      description: '', 
      isPurchase: 0, 
      endTime: '', 
      integral: 0, 
      lotteryTime: '',
      subtitle: '',
      tips: '',
      statusStr: '',
      orderSn: '',
      isRenewal: 0,
      button: '',
      quota: 0,
      purchaseQuantity: 0
    ),
    meta: BlindboxProductsMeta(
      status: []
    )
  ).obs;

  int blindBoxId = 0;
  
  @override
  void onReady() {
    Map<String, dynamic> mapArguments = Get.arguments;
    blindBoxId = mapArguments['id'];
    getBlindboxProductDetail();
    super.onReady();
  }

  /// 获取盲盒详情
  getBlindboxProductDetail({ bool showLoading = true }) async {
    BlindboxProductsDetailVo blindboxProductsDetailVo = await HttpUtilService.getBlindboxProductDetail(id: blindBoxId, loading: showLoading);
    data.value = blindboxProductsDetailVo;
    update();
  }

  /// 提交订单
  submitOrder() async {
    if (data.value.data!.quota! <= data.value.data!.purchaseQuantity!) {
      Get.dialog(MessageDialog(
        message: '提示：今日${data.value.data!.quota}次机会已经用完',
        // height: 149.h,
      ));
      return;
    }

    Get.dialog(DelDialog(message: '支付${data.value.data!.price}感恩值拆盲盒', onTap: () async {
      blindBoxPay(payAmount: data.value.data!.price!);
    }));
  }

  /// 盲盒支付
  blindBoxPay({required String payAmount}) {
    if (commonPageController.userData.value.isPayPassword! == 0) {
      Get.dialog(DelDialog(
        message: '请先设置支付密码', 
        onTap: () {
          Get.toNamed(
            '${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.UPDATE_PAY_PASSWORD_PAGE}',
            arguments: {
              'type': 1
            }
          );
        }
      ));
      return;
    }
    Get.dialog(
      UnionPayDialog(payPrice: '$payAmount', onSubmitted: (value) async {
        Get.back();
        BlindBoxSubmitOrderVo blindBoxSubmitOrderVo = await HttpUtilService.submitBlindBoxOrder(id: data.value.data!.id!, password: value);
        Get.offNamed(
          '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_DETAIL_PAGE}${AppRouters.BLINDBOX_PRIZE_PAGE}',
          arguments: {
            'id': blindBoxSubmitOrderVo.data!.id
          }
        );
      }),
      barrierDismissible: false
    );
  }
  
}